/**
 * 
 */
package cn.com.ytst.data.integration.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.ytst.data.integration.util.http.HttpClientPoolManager;

/**
 * 
 * @ClassName: HttpTools
 * @Description: 
 * @author 123774135@qq.com
 * @date 2019年1月25日 下午4:09:08
 *
 */
@Component("httptools")
public class HttpTools {

	@Autowired
	private HttpClientPoolManager httpClientPoolManager;
	
	private Logger logger = LoggerFactory.getLogger(HttpTools.class);
	
	
	private  static final   String UTF_TAG = "utf-8";

	/**
	 * 
	 * @Title: get @Description: get请求 @param @param path @param @param
	 *         clazz @param @return 设定文件 @return T 返回类型 @throws
	 */
	public String get(String path) {
		HttpGet httpget = new HttpGet(path);
		CloseableHttpResponse response = null;
		InputStream in = null;
		try {
			response = httpClientPoolManager.closeableHttpClient().execute(httpget);

			in = response.getEntity().getContent();

			return IOUtils.toString(in,UTF_TAG);
		
		} catch (IOException e) {
			logger.error("", e);
		} finally {
			closed(response,in);
		}
		return "";
	}

	private void setPostParams(HttpPost httpost, Map<String, Object> params) {
		List<NameValuePair> nvps = new ArrayList<>();
		Set<String> keySet = params.keySet();
		for (String key : keySet) {

			nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
		}
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, UTF_TAG));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * POST请求URL获取内容
	 * 
	 * @param url
	 * @return
	 * @author SHANHY
	 * @throws IOException
	 * @create 2015年12月18日
	 */
	public String post(String url, Map<String, Object> params) throws IOException {
		HttpPost httppost = new HttpPost(url);
		InputStream in = null;
		setPostParams(httppost, params);
		CloseableHttpResponse response = null;
		try {
			response = httpClientPoolManager.closeableHttpClient().execute(httppost, HttpClientContext.create());
			 in = response.getEntity().getContent();
			return IOUtils.toString(in,UTF_TAG);
		} catch (Exception e) {
			logger.error("", e);
		} finally {
			closed(response,in);
		}
		return null;
	}

	private void closed(CloseableHttpResponse response,InputStream in) {
		if (response != null) {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		IOUtils.closeQuietly(in);
	}
}
